package com.silvtar.sync.ui;

import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;

import com.silvtar.sync.controller.SilverySyncInit;
import com.jgoodies.forms.layout.FormLayout;
import com.jgoodies.forms.layout.ColumnSpec;
import com.jgoodies.forms.factories.FormFactory;
import com.jgoodies.forms.layout.RowSpec;

public class TransportServicePanel extends JPanel implements ActionListener {

    // 元素
    private JLabel          isyncdState;
    private JButton         startIsyncdButton;
    private JButton         shutdownIsyncdButton;

    private JLabel          ftpdState;
    private JButton         startFtpdButton;
    private JButton         shutdownFtpdButton;

    private JLabel          rsyncdState;
    private JButton         startRsyncdButton;
    private JButton         shutdownRsyncdButton;

    private JPanel          panel;

    // 对象
    private SilverySyncInit ssInit;
    private JButton         refreshButton;

    /**
     * Create the panel.
     */
    public TransportServicePanel(SilverySyncInit ssInit) {
        this.ssInit = ssInit;
        setPreferredSize(new Dimension(300, 250));
        setLayout(new BoxLayout(this, BoxLayout.Y_AXIS));

        JPanel validaterPanel = new JPanel();
        add(validaterPanel);
        validaterPanel.setPreferredSize(new Dimension(300, 230));

        startIsyncdButton = new JButton("启动");
        startIsyncdButton.addActionListener(this);
        validaterPanel.setLayout(new FormLayout(new ColumnSpec[] {
                ColumnSpec.decode("47px"),
                ColumnSpec.decode("75px"),
                FormFactory.LABEL_COMPONENT_GAP_COLSPEC,
                ColumnSpec.decode("75px"),},
            new RowSpec[] {
                FormFactory.RELATED_GAP_ROWSPEC,
                RowSpec.decode("16px"),
                FormFactory.UNRELATED_GAP_ROWSPEC,
                RowSpec.decode("29px"),
                FormFactory.RELATED_GAP_ROWSPEC,
                RowSpec.decode("16px"),
                FormFactory.RELATED_GAP_ROWSPEC,
                RowSpec.decode("29px"),
                FormFactory.RELATED_GAP_ROWSPEC,
                RowSpec.decode("16px"),
                FormFactory.RELATED_GAP_ROWSPEC,
                RowSpec.decode("29px"),}));

        JLabel validaterlabel = new JLabel("Isync：");
        validaterPanel.add(validaterlabel, "2, 2, center, center");

        isyncdState = new JLabel("未启动");
        validaterPanel.add(isyncdState, "4, 2, center, center");
        validaterPanel.add(startIsyncdButton, "2, 4, left, top");

        shutdownIsyncdButton = new JButton("关闭");
        validaterPanel.add(shutdownIsyncdButton, "4, 4, center, center");

        JLabel silverySyncLabel = new JLabel("FTP：");
        validaterPanel.add(silverySyncLabel, "2, 6, center, center");

        ftpdState = new JLabel("未启动");
        validaterPanel.add(ftpdState, "4, 6, center, center");

        startFtpdButton = new JButton("启动");
        validaterPanel.add(startFtpdButton, "2, 8, center, center");
        startFtpdButton.addActionListener(this);

        shutdownFtpdButton = new JButton("关闭");
        validaterPanel.add(shutdownFtpdButton, "4, 8, center, center");

        JLabel rsyncdLabel = new JLabel("Rsync：");
        validaterPanel.add(rsyncdLabel, "2, 10, center, center");

        rsyncdState = new JLabel("重新启动");
        validaterPanel.add(rsyncdState, "4, 10, center, center");

        startRsyncdButton = new JButton("启动");
        validaterPanel.add(startRsyncdButton, "2, 12, center, center");
        startRsyncdButton.addActionListener(this);

        shutdownRsyncdButton = new JButton("关闭");
        validaterPanel.add(shutdownRsyncdButton, "4, 12, center, center");

        panel = new JPanel();
        add(panel);

        refreshButton = new JButton("刷新服务状态");
        refreshButton.addActionListener(this);
        panel.add(refreshButton);

        // 逻辑
        flashServiceState();
    }

    public void flashServiceState() {
        // 验证服务
        if (ssInit.isIsyncOn()) {
            isyncdState.setText("已启动");
            startIsyncdButton.setText("重新启动");
        } else {
            isyncdState.setText("未启动");
            startIsyncdButton.setText("启动");
        }
        // rsync
        if (ssInit.isRsyncdOn()) {
            rsyncdState.setText("已启动");
            startRsyncdButton.setText("重新启动");
        } else {
            rsyncdState.setText("未启动");
            startRsyncdButton.setText("启动");
        }
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        // Isync服务
        if (e.getSource().equals(startIsyncdButton)) {
            ssInit.startIsyncDaemon();
        }
        // Ftp服务
        if (e.getSource().equals(startFtpdButton)) {

        }
        // Rsync服务
        if (e.getSource().equals(startRsyncdButton)) {
            ssInit.startRsyncDaemon();
        }
        // 刷新服务状态
        if (e.getSource().equals(refreshButton)) {
            ssInit.refreshDaemonState();
        }
        flashServiceState();
    }

}
